package com.wang.util;


import com.wang.bean.Result;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class FileUtil {

    public static String getFilePath(){
        return Configure.getOutPath();
    }

    public static Result downLoadMp4(HttpServletResponse response, String filename) throws UnsupportedEncodingException {
        Result re=new Result();
        String filePath = Configure.getOutPath() ;
        File file = new File(filePath + "/" + filename);
        if(file.exists()){ //判断文件父目录是否存在
            //response.setContentType("video/mpeg4;charset=UTF-8");
           // response.setCharacterEncoding("UTF-8");
            // response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode(filename,"UTF-8"));
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;
            OutputStream os = null; //输出流
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while(i != -1){
                    os.write(buffer,0,i);
                    i = bis.read(buffer);
                }
            } catch (Exception e) {
                re.setCode("204");
                return re;
            }
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                re.setCode("205");
                return re;
            }
            re.setCode("206");
            return re;
        }
        else
        {
            System.out.println("not fond!");
        }
        re.setCode("204");
        return re;
    }




    public static Result downLoadapp(HttpServletResponse response, String filename) throws UnsupportedEncodingException {
        Result re=new Result();
        String filePath = Configure.getAppPath();
        File file = new File(filePath + "/" + filename);
        if(file.exists()){ //判断文件父目录是否存在
            //response.setContentType("video/mpeg4;charset=UTF-8");
            // response.setCharacterEncoding("UTF-8");
            // response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode(filename,"UTF-8"));
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;
            OutputStream os = null; //输出流
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while(i != -1){
                    os.write(buffer,0,i);
                    i = bis.read(buffer);
                }
            } catch (Exception e) {
                re.setCode("204");
                return re;
            }
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                re.setCode("205");
                return re;
            }
            re.setCode("206");
            return re;
        }
        else
        {
            System.out.println("not fond!");
        }
        re.setCode("204");
        return re;
    }
}